package com.qidian.md.service.impl;

import com.qidian.md.mapper.VendorMapper;
import com.qidian.md.service.VendorService;
import com.qidian.mes.pojo.md.Vendor;
import com.qidian.mes.util.pojo.MESResult;
import com.qidian.mes.util.pojo.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author 18林梦雅
 * @Company 起点编程
 * @Time 2023/10/30 19:44
 * @Description
 */
@Service
public class VendorServiceImpl implements VendorService {
    @Autowired
    private VendorMapper vendorMapper;



    @Override
    public MESResult queryByPage(Vendor vendor, int page, int limit) {
        Pager<Vendor> pager=new Pager<>();
        pager.setCurr(page);
        pager.setLimit(limit);
        pager.setTotal( vendorMapper.selectCount(vendor));
        pager.setData(vendorMapper.selectByPage(vendor,(page-1)*limit,limit));
        pager.setMaxPageNum((int) Math.ceil(pager.getTotal()*1.0/limit));
        return MESResult.ok(pager);

    }



    /**
     * 添加供应商,先判断供应商名是否重复,再添加
     * @param vendor
     * @return
     */
    @Override
    public MESResult addVendor(Vendor vendor) {
//
        return MESResult.updateIsOk(vendorMapper.addVendor(vendor));
    }



    /**
     * 先根据Id查询内容,返回给前端的编辑页面
     * @param vendorId
     * @return
     */
    @Override
    public MESResult queryById(Long vendorId) {
        Vendor vendor = null;
        if ((vendor = vendorMapper.selectById(vendorId)) != null){
            return MESResult.ok(vendor);
        }
        return MESResult.empty();
    }
    /**
     * 根据供应商id-->供应商修改编辑
     * 直接更新数据,不用判断供应商名是否重复,编码是不一样的
     * @param vendor
     * @return
     */
    @Override
    public MESResult update(Vendor vendor) {

        return MESResult.updateIsOk(vendorMapper.update(vendor));

    }


    /**
     * 单行删除功能
     * @param vendorId
     * @return
     */
    @Override
    public MESResult deleteUpdateStatus(Long vendorId) {
        return MESResult.ok(vendorMapper.deleteUpdateStatus(vendorId));
    }




}
